﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emergency4.Session;

namespace Emergency4.Dal
{/// <summary>
 /// 员工窗口数据查询
 /// </summary>
 /// <returns></returns>
    internal class AssetDal
    {
        static Session2Entities en = new Session2Entities();
        //查询所有的资产信息
        internal static List<Hashtable> findAllAsset()
        {
            List<Asset> assetList = en.Assets.ToList();
            List<Hashtable> list = new List<Hashtable>();
            EmergencyMaintenance em = new EmergencyMaintenance();
            foreach (Asset asset in assetList)
            {
                Hashtable h = new Hashtable();
                //资产id
                int y = Convert.ToInt32(asset.ID);
                em = en.EmergencyMaintenances.Where(x => x.AssetID == y).OrderByDescending(x => x.EMEndDate).FirstOrDefault();
                if(em != null)
                {
                    int count = en.EmergencyMaintenances.Where((x) => x.AssetID == y && x.EMEndDate == null).Count();
                    int countEM = en.EmergencyMaintenances.Where(x => x.AssetID == y && x.EMEndDate != null).Count();
                    Boolean flag = true;
                    if (count > 0)
                    {
                        flag = false;
                    }
                    h.Add("flag", flag);
                    String date = null;
                   
                    try
                    {
                        if (em.EMStartDate.ToString() != "" || em.EMStartDate.ToString() != null)
                        {
                            //使用try-catch抛异常，原因是开始日期可能是空的，所有要先判断一下
                            //DateTime d = （DateTime）em.EMStartDate;
                           // d.ToString("yyyy-MM-dd");
                            date = em.EMStartDate.ToString().Substring(0, 9);
                        }
                    }
                    catch (Exception ex)
                    {

                    }
                    h.Add("date", date);
                    h.Add("countEM", countEM);
                }
                else
                {
                    h.Add("flag", true);
                    h.Add("date", null);
                    h.Add("countEM", 0);
                }
                h.Add("id",asset.ID);
                h.Add("assetName",asset.AssetName);
                h.Add("assetSN",asset.AssetSN);
                list.Add(h);
            }
            return list;
        }
        //添加紧急情况信息
        internal static void AddEMergency(EmergencyMaintenance ecy)
        {
            //向数据库中添加 
            en.EmergencyMaintenances.Add(ecy);
            en.SaveChanges();//保存紧急情况
        }

        //通过名称查询 id
        internal static long findByPriorityName(string priorityName)
        {
          return en.Priorities.Where(o => o.Name == priorityName).FirstOrDefault().ID;
        }

        //查询所有的
        internal static List<Priority> findByAllPriority()
        {
          return en.Priorities.ToList();
        }

        //通过id查询资产 一条信息
        internal static Asset findByAssetId(int id)
        {
            return en.Assets.Where(o => o.ID == id).FirstOrDefault();
        }
    }
}
